import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;

public class StorageManageFrame extends JFrame {
    private JTable storageTable;
    private DefaultTableModel tableModel;

    public StorageManageFrame() {
        setTitle("库存管理");
        setSize(800, 500);
        setLocationRelativeTo(null);

        // 表格模型
        tableModel = new DefaultTableModel();
        tableModel.addColumn("货物编号");
        tableModel.addColumn("货物名称");
        tableModel.addColumn("仓位编号");
        tableModel.addColumn("仓位名称");
        tableModel.addColumn("库存数量");
        tableModel.addColumn("最后更新日期");

        storageTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(storageTable);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton addBtn = new JButton("入库");
        JButton outBtn = new JButton("出库");
        JButton transferBtn = new JButton("调拨");
        JButton refreshBtn = new JButton("刷新");

        buttonPanel.add(addBtn);
        buttonPanel.add(outBtn);
        buttonPanel.add(transferBtn);
        buttonPanel.add(refreshBtn);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        loadStorageData();

        // 事件监听
        addBtn.addActionListener(e -> {
            new StorageInDialog().setVisible(true);
            loadStorageData();
        });

        outBtn.addActionListener(e -> {
            new StorageOutDialog().setVisible(true);
            loadStorageData();
        });

        transferBtn.addActionListener(e -> {
            new StorageTransferDialog().setVisible(true);
            loadStorageData();
        });

        refreshBtn.addActionListener(e -> loadStorageData());
    }

    private void loadStorageData() {
        tableModel.setRowCount(0);

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(
                     "SELECT s.dno, g.dname, s.wno, w.wname, s.num, s.savedate " +
                             "FROM storage s JOIN goods g ON s.dno = g.dno " +
                             "JOIN warehouse w ON s.wno = w.wno")) {

            while (rs.next()) {
                tableModel.addRow(new Object[]{
                        rs.getString("dno"),
                        rs.getString("dname"),
                        rs.getString("wno"),
                        rs.getString("wname"),
                        rs.getInt("num"),
                        rs.getDate("savedate")
                });
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}

class StorageInDialog extends JDialog {
    // 实现入库对话框
    // 包含字段：dno(货物), wno(仓位), num(数量)
}

class StorageOutDialog extends JDialog {
    // 实现出库对话框
    // 包含字段：dno(货物), wno(仓位), num(数量)
}

class StorageTransferDialog extends JDialog {
    // 实现调拨对话框
    // 包含字段：dno(货物), fromWno(源仓位), toWno(目标仓位), num(数量)
}